package com.ruoyi.commerce.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.commerce.domain.CommerceOrders;
import com.ruoyi.commerce.domain.bo.CommerceOrdersBo;
import com.ruoyi.commerce.domain.vo.CommerceOrdersVo;
import com.ruoyi.commerce.mapper.CommerceOrdersMapper;
import com.ruoyi.commerce.service.ICommerceOrdersService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 订单Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-15
 */
@RequiredArgsConstructor
@Service
public class CommerceOrdersServiceImpl implements ICommerceOrdersService {

    private final CommerceOrdersMapper baseMapper;

    /**
     * 查询订单
     */
    @Override
    public CommerceOrdersVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询订单
     */
    @Override
    public List<CommerceOrdersVo> queryByIds(Collection<Long> ids){
        return baseMapper.selectVoBatchIds(ids);
    }

    /**
     * 查询订单列表
     */
    @Override
    public TableDataInfo<CommerceOrdersVo> queryPageList(CommerceOrdersBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CommerceOrders> lqw = buildQueryWrapper(bo);
        Page<CommerceOrdersVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询订单列表
     */
    @Override
    public List<CommerceOrdersVo> queryList(CommerceOrdersBo bo) {
        LambdaQueryWrapper<CommerceOrders> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 根据采购订单查询订单id列表
     */
    @Override
    public List<CommerceOrdersVo> queryListByPurchaseId(Long purchaseId) {
        LambdaQueryWrapper<CommerceOrders> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(CommerceOrders::getPurchaseId, purchaseId);
        return baseMapper.selectVoList(queryWrapper);
    }

    /**
     * 根据用户id查询订单列表
     */
    @Override
    public List<CommerceOrdersVo> queryListByUserId(Long userId) {
        LambdaQueryWrapper<CommerceOrders> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(CommerceOrders::getUserId, userId);
        return baseMapper.selectVoList(queryWrapper);
    }

    /**
     * 批量更新订单信息
     *
     * @param ids      订单ID列表
     * @param updates  要更新的字段和对应的新值
     * @return         是否成功更新订单信息
     */
    @Override
    public Boolean batchUpdateOrderInfo(Collection<Long> ids, Map<String, Object> updates) {
        UpdateWrapper<CommerceOrders> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", ids);

        for (Map.Entry<String, Object> entry : updates.entrySet()) {
            String fieldName = entry.getKey();
            Object newValue = entry.getValue();

            updateWrapper.set(fieldName, newValue);
        }

        int count = baseMapper.update(null, updateWrapper);

        return count == ids.size();
    }

    private LambdaQueryWrapper<CommerceOrders> buildQueryWrapper(CommerceOrdersBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CommerceOrders> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPurchaseId() != null, CommerceOrders::getPurchaseId, bo.getPurchaseId());
        lqw.eq(bo.getUserId() != null, CommerceOrders::getUserId, bo.getUserId());
        lqw.eq(bo.getOrderDate() != null, CommerceOrders::getOrderDate, bo.getOrderDate());
        lqw.eq(bo.getProductId() != null, CommerceOrders::getProductId, bo.getProductId());
        lqw.eq(bo.getProductQuantity() != null, CommerceOrders::getProductQuantity, bo.getProductQuantity());
        lqw.eq(bo.getTotalPrice() != null, CommerceOrders::getTotalPrice, bo.getTotalPrice());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), CommerceOrders::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增订单
     */
    @Override
    public Boolean insertByBo(CommerceOrdersBo bo) {
        CommerceOrders add = BeanUtil.toBean(bo, CommerceOrders.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改订单
     */
    @Override
    public Boolean updateByBo(CommerceOrdersBo bo) {
        CommerceOrders update = BeanUtil.toBean(bo, CommerceOrders.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CommerceOrders entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除订单
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
